{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ebd8c45e",
   "metadata": {},
   "source": [
    "### 运行之前，请先看[前置工作](./00-construct_MT-Datasets.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "757efb17",
   "metadata": {},
   "source": [
    "## 这里是为对segs做进一步处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6cd0089f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import glob\n",
    "import numpy as np\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4fb85c68",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_root = './res/user_MT-Dataset/'\n",
    "ori_segs_dir = os.path.join(data_root, 'segs')\n",
    "dst_segs_dir = os.path.join(data_root, 'scgan_segs')\n",
    "if not os.path.exists(dst_segs_dir):\n",
    "    os.makedirs(dst_segs_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bbbfdc7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def parsing(origin_path, new_path):\n",
    "    seg = np.array(Image.open(origin_path))\n",
    "    new = np.zeros_like(seg)\n",
    "    new[seg == 0] = 0\n",
    "    new[seg == 1] = 4\n",
    "    new[seg == 2] = 7\n",
    "    new[seg == 3] = 2\n",
    "    new[seg == 4] = 6\n",
    "    new[seg == 5] = 1\n",
    "    new[seg == 6] = 8\n",
    "    new[seg == 7] = 9\n",
    "    new[seg == 8] = 11\n",
    "    new[seg == 9] = 13\n",
    "    new[seg == 10] = 12\n",
    "    new[seg == 11] = 3\n",
    "    new[seg == 12] = 5\n",
    "    new[seg == 13] = 10\n",
    "    img = Image.fromarray(new)\n",
    "    img.save(new_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b37008fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./res/user_MT-Dataset/segs/makeup/shenaojun.png\n",
      "./res/user_MT-Dataset/segs/makeup/xiangtai.png\n",
      "./res/user_MT-Dataset/segs/non-makeup/shuchang.png\n",
      "./res/user_MT-Dataset/segs/non-makeup/yuanli.png\n"
     ]
    }
   ],
   "source": [
    "origin_segs_next_dirs = glob.glob('%s/*' %(ori_segs_dir))\n",
    "for origin_segs_next_dir in origin_segs_next_dirs:\n",
    "    directory_name = os.path.basename(origin_segs_next_dir)\n",
    "    new_segs_next_dir = os.path.join(dst_segs_dir, directory_name)\n",
    "    os.makedirs(new_segs_next_dir, exist_ok=True)\n",
    "    origin_segs_paths = glob.glob('%s/*' %(origin_segs_next_dir))\n",
    "    for origin_segs_path in origin_segs_paths:\n",
    "        print(origin_segs_path)\n",
    "        basename = os.path.basename(origin_segs_path)\n",
    "        new_segs_path = os.path.join(new_segs_next_dir, basename)\n",
    "        parsing(origin_segs_path, new_segs_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45ae92ce",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
